package org.bit.lesson03;

import java.util.concurrent.TimeUnit;

public class Demo301 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                System.out.println("hello thread...");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        // 查看启动之前的线程状态
        System.out.println("启动之前：" + thread.getState());
        System.out.println("是否存活：" + thread.isAlive());
        // 启动线程
        thread.start();
        // 查看状态之后等一会儿
        TimeUnit.MICROSECONDS.sleep(500);
        // 启动之后
        System.out.println("启动之后：" + thread.getState());
        System.out.println("是否存活：" + thread.isAlive());
        // 等待线程执行完成
        thread.join();
        // 线程结束之后查看状态
        System.out.println("结束之后：" + thread.getState());
        System.out.println("是否存活：" + thread.isAlive());
    }
}
